package com.qf.hsr.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hsr.dao.IUserMapper;
import com.qf.hsr.entity.ResultVO;
import com.qf.hsr.pojo.User;
import com.qf.hsr.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.qf.hsr.constant.MessageConstant.*;

/**
 * @author q
 * @version V1.0
 * @date 2021/11/16
 **/
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserMapper userMapper;

    @Override
    public ResultVO<List<User>> findAllUser() {
        List<User> allUser = userMapper.findAllUser();
        return ResultVO.ok(USER_QUERY_SUCCESS,allUser);
    }

    @Override
    public ResultVO deleteUserById(Integer userId) {
        Integer i = userMapper.deleteUserById(userId);
        if (i.intValue() == 1) {
            return ResultVO.ok(USER_DELETE_SUCCESS);
        }
        return ResultVO.error(USER_DELETE_FAIL);
    }

    @Override
    public ResultVO saveUser(User user) {
        if ("" != user.getPhone() && null != user.getPhone()){
            user.setPassword(user.getPhone().substring(7));
        } else {
            user.setPassword("123456");
        }
        user.setNickName(user.getUsername());
        user.setUserCreateTime(new Date());
        user.setUserUpdateTime(new Date());
        user.setIsDelete(0);
        System.out.println("saveUser" + user);
        Integer i = userMapper.saveUser(user);
        if (i.intValue() == 1) {
            return ResultVO.ok(USER_SAVE_SUCCESS);
        }
        return ResultVO.error(USER_SAVE_FAIL);
    }

    @Override
    public ResultVO findUserById(Integer id) {
        User user = userMapper.findUserById(id);
        return ResultVO.ok(USER_QUERY_SUCCESS,user);
    }

    @Override
    public ResultVO updateUser(User user) {
        user.setUserUpdateTime(new Date());
        Integer i = userMapper.updateUser(user);
        if (i == 1){
            return ResultVO.ok(USER_UPDATE_SUCCESS);
        } else {
            return ResultVO.error(USER_UPDATE_FAIL);
        }
    }

    @Override
    public ResultVO<List<User>> findUserByCondition(String condition) {
        List<User> userByCondition = userMapper.findUserByCondition(condition);
        return ResultVO.ok(USER_QUERY_SUCCESS,userByCondition);
    }

    @Override
    public ResultVO<PageInfo<User>> findAllWithPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> allUserWithPage = userMapper.findAllUser();
        System.out.println("allUserWithPage --> " + allUserWithPage);
        PageInfo<User> pageInfo = new PageInfo<>(allUserWithPage);
        System.out.println("pageInfo --> " + pageInfo);
        return ResultVO.ok(USER_QUERY_SUCCESS, pageInfo);
    }

    @Override
    public ResultVO<PageInfo<User>> findUserByConditionWithPage(String condition, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userByCondition = userMapper.findUserByCondition(condition);
        PageInfo<User> pageInfo = new PageInfo<>(userByCondition);
        return ResultVO.ok(USER_QUERY_SUCCESS, pageInfo);
    }
}
